package com.stm.bi.report.productTrend;

import com.stm.bi.dto.query.TableReportQry;
import com.stm.bi.model.DatatablePage;
import com.stm.bi.report.AbsDatatableQueryExe;
import com.stm.framework.dto.Response;
import com.stm.saas.api.ProductConfigRemoteService;
import com.stm.saas.dto.vo.ProductConfigVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import static com.stm.bi.constant.ProductTrendConstants.*;

@Component
public class ProductTrendTableQueryExe extends AbsDatatableQueryExe<TableReportQry, DatatablePage> {

    @Resource
    private StyleStructureReportQueryExe styleStructureDetailChart;
    @Resource
    private DigestibilityReportQueryExe digestibilityReportQueryExe;
    @Resource
    private ReturnReportQueryExe returnReportQueryExe;
    @Resource
    private StyleThreeSevenReportQueryExe styleThreeSevenReportExe;
    @Resource
    private StyleQtyReportQueryExe styleQtyReportQueryExe;
    @Resource
    private ProductConfigRemoteService productConfigRemoteService;

    // 从产品配置中获取销售类型
    private void setSalesTypeFromConfig(Long orgId, TableReportQry query) {
        Response<ProductConfigVO> confResp = productConfigRemoteService.selectByOrgId(orgId);
        String salesType = "dealing";
        if (confResp.isSuccess() && confResp.getData() != null && confResp.getData().getSalesDefinition() != null) {
            salesType = confResp.getData().getSalesDefinition();
        }
        query.setSalesType(salesType);
    }

    @Override
    public DatatablePage query(Long orgId, String orgNo, TableReportQry query) {
        // 获取销售类型配置
        setSalesTypeFromConfig(orgId, query);
        
        // 根据维度ID分发到具体的查询执行器
        return dispatchQuery(query);
    }
    
    // 根据维度ID分发查询请求到对应的执行器
    private DatatablePage dispatchQuery(TableReportQry query) {
        Long dimensionId = query.getDimensionId();
        
        // 商品款式结构数量查询
        if (PROD_STYLE_STRUCTURE_QTY.equals(dimensionId)) {
            return styleStructureDetailChart.styleStructureTable(query);
        }
        
        // 售罄率维度查询
        if (DIGESTIBILITY.equals(dimensionId)) {
            return digestibilityReportQueryExe.digestibilityDimensionTable(query);
        }
        
        // 退货率维度查询（品类/品类组合/款式）
        if (RETURN_DIMENSION_IDS.contains(dimensionId)) {
            return returnReportQueryExe.returnRateTable(query);
        }
        
        // 退货原因查询
        if (RETURN_REASON.equals(dimensionId)) {
            return returnReportQueryExe.returnReasonTable(query);
        }
        
        // 款式三七分查询
        if (STYLE_THREE_SEVEN.equals(dimensionId)) {
            return styleThreeSevenReportExe.threeSevenTable(query);
        }
        
        // 款式数量查询
        if (STYLE_QTY.equals(dimensionId)) {
            return styleQtyReportQueryExe.styleQtyTable(query);
        }
        
        // 未匹配到任何维度ID
        return null;
    }

    @Override
    public String getReportName() {
        return null;
    }

    @Override
    public boolean syncExport(DatatablePage page) {
        return false;
    }
}
